#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
#       lut_rvb.py
#       
#       Copyright 2009 ndavid <ndavid@DEL-9323825>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.


import numpy
import PIL
import colorsys

from PIL import Image
from numpy import array 

#---------------------------------------------
#  grayscale RVB
#---------------------------------------------
# w graysacle width ( NOT the direction of shaded off)
# h grayscale height  Direction of shaded off
# outputname : name of the output image , need extension
def grayscale(w, h, outputname ):
	grayscale = numpy.zeros((w,h,3), numpy.uint8)
	# initialze init and end value for grayscale
	grayscale[0:w,0]=[0,0,0]
	grayscale[0:w,h-1]=[255,255,255]
	# compute grayscale with linear interpolation
	begin_color = numpy.zeros((w,1,3), numpy.float32)
	end_color = numpy.zeros((w,1,3), numpy.float32)
	begin_color = grayscale[0:w,0]
	end_color = grayscale[0:w,h-1]
	tmp_color_value = numpy.zeros((w,1,3), numpy.float32)
	for i in range(1, h-1):
		t= i*1.0/(h-1)
		#print i, t
		tmp_color_value=(1-t)*begin_color+t*end_color
		grayscale[0:w,i] = tmp_color_value
		#print grayscale[0,i]

	gray_lut = Image.fromarray(grayscale, "RGB")
	gray_lut.save(outputname)


#---------------------------------------------
#  luminance scale HSL
#---------------------------------------------
# w graysacle width ( NOT the direction of shaded off)
# h grayscale height  Direction of shaded off
# hue hue value of the luminance scale
# outputname : name of the output image , need extension
def Lscale(w,h, hue, outputname):
	Lscale = numpy.zeros((w,h,3), numpy.uint16)
	# initialze init and end value for grayscale
	saturation = 255 # max 
	Lscale[0:w,0]=[hue,saturation,0]
	Lscale[0:w,h-1]=[hue,saturation,255]
	# compute Lscale with linear interpolation
	begin_color = numpy.zeros((w,1,3), numpy.float32)
	end_color = numpy.zeros((w,1,3), numpy.float32)
	begin_color = Lscale[0:w,0]
	end_color = Lscale[0:w,h-1]
	tmp_color_value = numpy.zeros((w,1,3), numpy.float32)
	for i in range(1, h-1):
		t= i*1.0/(h-1)
		#print i, t
		tmp_color_value=(1-t)*begin_color+t*end_color
		Lscale[0:w,i] = tmp_color_value
		#print Lscale[0,i]
	
	# convert HSV to RVB to export image
	LscaleRVB = numpy.zeros((w,h,3), numpy.uint8)
	for i in range(0, h):
		hue = Lscale[0,i,0]*1.0/360
		l = Lscale[0,i,2]*1.0/255
		s = Lscale[0,i,1]*1.0/255
		#print h, l ,s
		rvb = colorsys.hls_to_rgb(hue, l, s)
		#print rvb
		LscaleRVB[0:w,i]=[255*rvb[0],255*rvb[1],255*rvb[2]]
		
	L_lut = Image.fromarray(LscaleRVB, "RGB")
	L_lut.save(outputname)

def main():
	
	grayscale(32,256,"gray_lut.tiff")
	Lscale(32,256,0, "red_L_scale.tiff")
	Lscale(32,256,60, "yellow_L_scale.tiff")

	
	return 0
	
if __name__ == '__main__': main()
